/**
 *@Time    : 2021/11/3 20:58
 *@Author  : Shuai Jhou
 *@File    : context_timeout
 *@Software: GoLand
 */

package middleware

import (
	"context"
	"github.com/gin-gonic/gin"
	"time"
)

func ContextTimeout(t time.Duration) func(c *gin.Context) {
	return func(c *gin.Context) {
		ctx, cancel := context.WithTimeout(c.Request.Context(), t)
		defer cancel()
		c.Request = c.Request.WithContext(ctx)
		c.Next()
	}
}
